package com.shop.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.opensymphony.xwork2.ActionSupport;
import com.shop.entity.User;
import com.shop.service.UserService;

public class AppAction extends ActionSupport implements SessionAware,ServletRequestAware{

	private static final long serialVersionUID = 1L;

	private UserService userService = new UserService();
	
	private Map<String, Object> session;
	private User user;
	private String code;
	private String captcha;
	private HttpServletRequest request;
	
	public String execute() {
		System.out.println("execute.....");
		return "success";
	}
	
	public String login() {
		
		boolean result = SimpleImageCaptchaServlet.validateResponse(request, captcha);
		if(result) {
			System.out.println("login......");
			User u = userService.login(user);
			if(u == null) {
				code = "10001";
				return "login";
			} else {
				//�Ѷ������session��
				session.put("curr_user", u);
				return "success";
			}
		} else {
			code = "10009";
			return "login";
		}
	}
	
	public void validateLogin() {
		if(user == null || user.getUsername() == null || "".equals(user.getUsername()) || user.getPassword() == null || "".equals(user.getPassword())) {
			System.out.println("validateLogin error...");
			addActionError("�û�������벻��Ϊ��");
			code = "10002";
		}else if(user.getUsername().length() > 6) {
			addActionError("xxx");
			code = "10003";
		}
	}
	
	/*@Override
	public void validate() {
		System.out.println("validate.................");
		
	}*/
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	//get set
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	
	
	
}
